package com.example.demo.controller;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：徐志荣
 * @Date：2023/7/9 上午 10:30
 */
@RestController
public class RedisAnnController {

    /*
    * 存入和读取缓存
    * @param name
    * @param phone
    * @return
    */
    @RequestMapping("/ann-get")
    @Cacheable(value="spring.cache",key="#name+'-'+#phone")
    public String get(String name,String phone){
        if(!StringUtils.hasLength(name) ||!StringUtils.hasLength(phone)){
            return  null;
        }
        System.out.println("存入和读取缓存");
        return "name="+name + " | phone"+phone;
    }

    @RequestMapping("/ann-del")
    @CacheEvict(value="spring.cache",key="#name+'-'+#phone")
    public  void del(String name,String phone){
        System.out.println("执行了缓存删除");
    }

    /*
    * 更新了缓存
    * @param name
    * @param phone
    * @return
    */
    @RequestMapping("/ann-mod")
    @CachePut(value="spring.cache",key="#name+'-'+#phone")
    public String put(String name,String phone){
        if(!StringUtils.hasLength(name) ||!StringUtils.hasLength(phone)){
            return  null;
        }
        System.out.println("执行了put");
        return "name="+name + " : phone"+phone;
    }


}
